package logic

import (
	"context"
	"fmt"
	"netease/models/mysql"

	"netease/neteaserpc/internal/svc"
	"netease/neteaserpc/neteaserpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type RegisterLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewRegisterLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RegisterLogic {
	return &RegisterLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 注册
func (l *RegisterLogic) Register(in *neteaserpc.RegisterRequest) (*neteaserpc.RegisterResponse, error) {
	// todo: add your logic here and delete this line
	var user mysql.Netease
	mysql.DB.Where("account=?", in.Account).Find(&user)
	if user.ID != 0 {
		return nil, fmt.Errorf("用户已存在,请前去登录")
	}
	users := mysql.Netease{
		Account:  in.Account,
		Password: in.Password,
	}
	mysql.DB.Create(&users)
	return &neteaserpc.RegisterResponse{
		Id: int64(users.ID),
	}, nil
}
